<%@ Page language="vb" CodeFile="settleInvoice.aspx.vb" Inherits="settleInvoice_aspx_vb" %>

<%
    rowNumber = 1
    invoiceID = Request.QueryString("invoiceID")
    If Len(invoiceID) > 0 Then 
        invoiceID = CLng(invoiceID)
    End IF
    If (Request.QueryString("do")) = Nothing Then 
        subDo = Request.QueryString("subDo")
        If Len(invoiceID) > 0 Then 
            invoiceCreditAmount = 0
            dbOpen()
            RecSet = Conn.Execute("Select i.invoiceClaim, i.invoiceAmount, i.invoiceDate, p.partnerName, c.countryCurrency, c.decimals, co.company, ic.invoiceClaim As invoiceCreditAmount, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From ((((tblInvoice i INNER JOIN tblPartner p ON p.partnerID = i.partnerID) INNER JOIN tblCountry c ON c.countryID = i.countryID) INNER JOIN tblCompany co On co.countryID = i.countryID) LEFT JOIN tblInvoiceCredit ic ON ic.invoiceID = i.invoiceID) Where i.invoiceID = " & invoiceID & "")
            If Not RecSet.EOF Then 
                invoiceClaim = RecSet.Fields("invoiceClaim").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                partnerName = RecSet.Fields("partnerName").Value
                countryCurrency = RecSet.Fields("countryCurrency").Value
                decimals = RecSet.Fields("decimals").Value
                company = RecSet.Fields("company").Value
                invoiceCreditAmount = RecSet.Fields("invoiceCreditAmount").Value
                salesTax = RecSet.Fields("salesTax").Value
            End IF
            RecSet.Close()
            RecSet = Nothing
            RecSet = Conn.Execute("Select i.invoiceClaim, i.invoiceAmount, i.invoiceDate, p.partnerName, c.countryCurrency, c.decimals, co.company, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (((tblInvoicePriceListModification i INNER JOIN tblPartner p ON p.partnerID = i.partnerID) INNER JOIN tblCountry c ON c.countryID = i.countryID) INNER JOIN tblCompany co On co.countryID = i.countryID) Where invoicePriceListModificationID = " & invoiceID & "")
            If Not RecSet.EOF Then 
                invoiceClaim = RecSet.Fields("invoiceClaim").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                partnerName = RecSet.Fields("partnerName").Value
                salesTax = RecSet.Fields("salesTax").Value
                decimals = RecSet.Fields("decimals").Value
                company = RecSet.Fields("company").Value
                salesTax = RecSet.Fields("salesTax").Value
            End IF
            RecSet.Close()
            RecSet = Nothing
            RecSet = Conn.Execute("Select i.invoiceClaim, i.invoiceAmount, i.invoiceDate, p.partnerName, c.countryCurrency, c.decimals, co.company, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From ((((tblInvoiceDurationPenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) INNER JOIN tblCountry c ON c.countryID = v.countryID) INNER JOIN tblCompany co On co.countryID = v.countryID) Where invoiceDurationPenaltyID = " & invoiceID & "")
            If Not RecSet.EOF Then 
                invoiceClaim = RecSet.Fields("invoiceClaim").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                partnerName = RecSet.Fields("partnerName").Value
                countryCurrency = RecSet.Fields("countryCurrency").Value
                decimals = RecSet.Fields("decimals").Value
                company = RecSet.Fields("company").Value
                salesTax = RecSet.Fields("salesTax").Value
            End IF
            RecSet.Close()
            RecSet = Nothing
            RecSet = Conn.Execute("Select i.invoiceClaim, i.invoiceAmount, i.invoiceDate, p.partnerName, c.countryCurrency, c.decimals, co.company, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From ((((tblInvoiceCreditDurationPenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) INNER JOIN tblCountry c ON c.countryID = v.countryID) INNER JOIN tblCompany co On co.countryID = v.countryID) Where invoiceCreditDurationPenaltyID = " & invoiceID & "")
            If Not RecSet.EOF Then 
                invoiceClaim = RecSet.Fields("invoiceClaim").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                partnerName = RecSet.Fields("partnerName").Value
                countryCurrency = RecSet.Fields("countryCurrency").Value
                decimals = RecSet.Fields("decimals").Value
                company = RecSet.Fields("company").Value
                salesTax = RecSet.Fields("salesTax").Value
            End IF
            RecSet.Close()
            RecSet = Nothing
            RecSet = Conn.Execute("Select i.invoiceClaim, i.invoiceAmount, i.invoiceDate, p.partnerName, c.countryCurrency, c.decimals, co.company, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From ((((tblInvoiceDistancePenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) INNER JOIN tblCountry c ON c.countryID = v.countryID) INNER JOIN tblCompany co On co.countryID = v.countryID) Where invoiceDistancePenaltyID = " & invoiceID & "")
            If Not RecSet.EOF Then 
                invoiceClaim = RecSet.Fields("invoiceClaim").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                partnerName = RecSet.Fields("partnerName").Value
                countryCurrency = RecSet.Fields("countryCurrency").Value
                decimals = RecSet.Fields("decimals").Value
                company = RecSet.Fields("company").Value
                salesTax = RecSet.Fields("salesTax").Value
            End IF
            RecSet.Close()
            RecSet = Nothing
            RecSet = Conn.Execute("Select i.invoiceClaim, i.invoiceAmount, i.invoiceDate, p.partnerName, c.countryCurrency, c.decimals, co.company, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From ((((tblInvoiceCreditDistancePenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) INNER JOIN tblCountry c ON c.countryID = v.countryID) INNER JOIN tblCompany co On co.countryID = v.countryID) Where invoiceCreditDistancePenaltyID = " & invoiceID & "")
            If Not RecSet.EOF Then 
                invoiceClaim = RecSet.Fields("invoiceClaim").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                partnerName = RecSet.Fields("partnerName").Value
                countryCurrency = RecSet.Fields("countryCurrency").Value
                decimals = RecSet.Fields("decimals").Value
                company = RecSet.Fields("company").Value
                salesTax = RecSet.Fields("salesTax").Value
            End IF
            RecSet.Close()
            RecSet = Nothing
            RecSet = Conn.Execute("Select i.invoiceClaim, i.invoiceAmount, i.invoiceDate, p.partnerName, c.countryCurrency, c.decimals, co.company, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (((tblInvoiceAdminFee i INNER JOIN tblPartner p ON p.partnerID = i.partnerID) INNER JOIN tblCountry c ON c.countryID = i.countryID) INNER JOIN tblCompany co On co.countryID = i.countryID) Where invoiceAdminFeeID = " & invoiceID & "")
            If Not RecSet.EOF Then 
                invoiceClaim = RecSet.Fields("invoiceClaim").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                partnerName = RecSet.Fields("partnerName").Value
                countryCurrency = RecSet.Fields("countryCurrency").Value
                decimals = RecSet.Fields("decimals").Value
                company = RecSet.Fields("company").Value
                salesTax = RecSet.Fields("salesTax").Value
            End IF
            RecSet.Close()
            RecSet = Nothing
            RecSet = Conn.Execute("Select i.invoiceClaim, i.invoiceAmount, i.invoiceDate, p.partnerName, c.countryCurrency, c.decimals, co.company, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From ((((tblInvoiceServiceAccountBalance i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) INNER JOIN tblCountry c ON c.countryID = v.countryID) INNER JOIN tblCompany co On co.countryID = v.countryID) Where invoiceServiceAccountBalanceID = " & invoiceID & "")
            If Not RecSet.EOF Then 
                invoiceClaim = RecSet.Fields("invoiceClaim").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                partnerName = RecSet.Fields("partnerName").Value
                countryCurrency = RecSet.Fields("countryCurrency").Value
                decimals = RecSet.Fields("decimals").Value
                company = RecSet.Fields("company").Value
                salesTax = RecSet.Fields("salesTax").Value
            End IF
            RecSet.Close()
            RecSet = Nothing
            RecSet = Conn.Execute("Select i.invoiceClaim, i.invoiceAmount, i.invoiceDate, p.partnerName, c.countryCurrency, c.decimals, co.company, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From ((((tblInvoiceCreditServiceAccountBalance i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) INNER JOIN tblCountry c ON c.countryID = v.countryID) INNER JOIN tblCompany co On co.countryID = v.countryID) Where invoiceCreditServiceAccountBalanceID = " & invoiceID & "")
            If Not RecSet.EOF Then 
                invoiceClaim = RecSet.Fields("invoiceClaim").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                partnerName = RecSet.Fields("partnerName").Value
                countryCurrency = RecSet.Fields("countryCurrency").Value
                decimals = RecSet.Fields("decimals").Value
                company = RecSet.Fields("company").Value
                salesTax = RecSet.Fields("salesTax").Value
            End IF
            RecSet.Close()
            RecSet = Nothing
            RecSet = Conn.Execute("Select i.invoiceClaim, i.invoiceAmount, i.invoiceDate, p.partnerName, c.countryCurrency, c.decimals, co.company, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (((tblInvoiceSeparate i INNER JOIN tblPartner p ON p.partnerID = i.partnerID) INNER JOIN tblCountry c ON c.countryID = i.countryID) INNER JOIN tblCompany co On co.countryID = i.countryID) Where invoiceSeparateID = " & invoiceID & "")
            If Not RecSet.EOF Then 
                invoiceClaim = RecSet.Fields("invoiceClaim").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                partnerName = RecSet.Fields("partnerName").Value
                countryCurrency = RecSet.Fields("countryCurrency").Value
                decimals = RecSet.Fields("decimals").Value
                company = RecSet.Fields("company").Value
                salesTax = RecSet.Fields("salesTax").Value
            End IF
            RecSet.Close()
            RecSet = Nothing
            RecSet = Conn.Execute("Select i.invoiceClaim, i.invoiceAmount, i.invoiceDate, p.partnerName, c.countryCurrency, c.decimals, co.company, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (((tblInvoiceCreditSeparate i INNER JOIN tblPartner p ON p.partnerID = i.partnerID) INNER JOIN tblCountry c ON c.countryID = i.countryID) INNER JOIN tblCompany co On co.countryID = i.countryID) Where invoiceCreditSeparateID = " & invoiceID & "")
            If Not RecSet.EOF Then 
                invoiceClaim = RecSet.Fields("invoiceClaim").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                partnerName = RecSet.Fields("partnerName").Value
                countryCurrency = RecSet.Fields("countryCurrency").Value
                decimals = RecSet.Fields("decimals").Value
                company = RecSet.Fields("company").Value
                salesTax = RecSet.Fields("salesTax").Value
            End IF
            RecSet.Close()
            RecSet = Nothing
            dbClose()
            If Len(invoiceClaim) = 0 Then 
                Response.Redirect("settleInvoice.aspx")
            End IF
            If IsDBNull(invoiceCreditAmount) = false Then 
                invoiceAmount = CDbl(invoiceAmount) - CDbl(invoiceCreditAmount)
                invoiceClaim = CDbl(invoiceClaim) - CDbl(invoiceCreditAmount)
            End IF
            invoiceSalesTaxAmount = (CDbl(invoiceAmount) * CDbl(salesTax)) / 100
            invoiceAmount = invoiceAmount + invoiceSalesTaxAmount
            invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
            invoiceAmount = CDbl(invoiceAmount)
            claimSalesTaxAmount = (CDbl(invoiceClaim) * CDbl(salesTax)) / 100
            invoiceClaim = invoiceClaim + claimSalesTaxAmount
            invoiceClaim = FormatNumber(CDbl(invoiceClaim), decimals)
            invoiceClaim = CDbl(invoiceClaim)
        End IF
        Over()
        strJS = ""
        strJS = strJS + "[""paymentAmount"", ""==''""],"
        strJS = strJS + "[""paymentDateYear"", ""<1""],"
        strJS = strJS + "[""paymentDateMonth"", ""<1""],"
        strJS = strJS + "[""paymentDateDay"", ""<1""]"
        checkFields()
        realTime = Now()
        year = Left(realTime, 4)
        month = Mid(realTime, 6, 2)
        day = Mid(realTime, 9, 2)
        Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='100%'>")
        Response.Write("<tr>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='3'>SETTLE AN INVOICE<font color='#fcbc00'>" & subDo & "</font><hr class='hrHeadline'></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Invoice ID:</td>")
        If Len(invoiceID) = 0 Then 
            Response.Write("<form action='settleInvoice.aspx?do=changeInvoiceID' method='post'>")
            Response.Write("<td align='right'>")
            Response.Write("<input type='text' name='invoiceID' id='invoiceID' value='" & invoiceID & "' size='29' maxlength='50' onkeyup='re=/(\d*)/; re.exec(this.value); this.value=RegExp.$1;'>")
            Response.Write("<input type='submit' value='Search'>")
            Response.Write("</td>")
            Response.Write("</form>")
        Else
            Response.Write("<td align='right'><input type='text' name='invoiceID' id='invoiceID' value='" & invoiceID & "' size='40' maxlength='50' readonly='readonly'></td>")
        End IF
        Response.Write("</tr>")
        Response.Write("<form action='settleInvoice.aspx?do=save&invoiceID=" & invoiceID & "' method='post'>")
        Response.Write("<tr><td class='formBold'>Partner:</td><td align='right'><input type='text' name='partnerName' id='partnerName' value='" & partnerName & "' size='40' maxlength='50' readonly='readonly'></td></tr>")
        Response.Write("<tr><td class='formBold'>Company:</td><td align='right'><input type='text' name='company' id='company' value='" & company & "' size='40' maxlength='50' readonly='readonly'></td></tr>")
        Response.Write("<tr><td class='formBold'>Invoice amount (" & countryCurrency & "):</td><td align='right'><input type='text' name='invoiceAmount' id='invoiceAmount' value='" & invoiceAmount & "' size='40' maxlength='50' readonly='readonly'></td></tr>")
        Response.Write("<tr><td class='formBold'>Invoice claim (" & countryCurrency & "):</td><td align='right'><input type='text' name='invoiceClaim' id='invoiceClaim' value='" & invoiceClaim & "' size='40' maxlength='50' readonly='readonly'></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Payment amount (" & countryCurrency & "):</td>")
        Response.Write("<td align='right'><input type='text' name='paymentAmount' id='paymentAmount' value='" & invoiceClaim & "' size='40' maxlength='50' onkeyup='re=/([\d\,]*)/; re.exec(this.value); this.value=RegExp.$1;'></td>")
        Response.Write("<td><div id='error_paymentAmount' class='edit_error'>&laquo; OBS!<br></div></td>")
        Response.Write("</tr>")
        Response.Write("<tr><td class='formBold'>Invoice date:</td><td align='right'><input type='text' name='invoiceDate' id='invoiceDate' value='" & invoiceDate & "' size='40' maxlength='50' readonly='readonly'></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Payment date:&nbsp; </td>")
        Response.Write("<td align='right'>")
        Response.Write("<select name='paymentDateYear' id='paymentDateYear'>")
        Response.Write("<option value=''>&nbsp; Year ")
        stopYear = CDbl(year) + 3
        yearCounter = year
        For yearLoop = year To stopYear
            Response.Write("<option value='" & yearCounter & "'")
            If CStr(yearCounter) = year Then 
                Response.Write("selected='selected'")
            End IF
            Response.Write("> " & yearCounter & " ")
            yearCounter = yearCounter + 1
        Next
        Response.Write("</select>")
        Response.Write(" - ")
        Response.Write("<select name='paymentDateMonth' id='paymentDateMonth'>")
        Response.Write("<option value=''>&nbsp; Month ")
        yearCounter = 1
        For yearLoop = 1 To 12
            Response.Write("<option value='" & yearCounter & "'")
            If CLng(yearCounter) = CLng(month) Then 
                Response.Write("selected='selected'")
            End IF
            Response.Write("> " & yearCounter & " ")
            yearCounter = yearCounter + 1
        Next
        Response.Write("</select>")
        Response.Write(" - ")
        Response.Write("<select name='paymentDateDay' id='paymentDateDay'>")
        Response.Write("<option value=''>&nbsp; Day&nbsp; ")
        yearCounter = 1
        For yearLoop = 1 To 31
            Response.Write("<option value='" & yearCounter & "'")
            If CLng(yearCounter) = CLng(day) Then 
                Response.Write("selected='selected'")
            End IF
            Response.Write("> " & yearCounter & " ")
            yearCounter = yearCounter + 1
        Next
        Response.Write("</select>")
        Response.Write("</td>")
        Response.Write("<td>")
        Response.Write("<div id='error_paymentDateYear' class='edit_error'>&laquo; OBS!<br></div>")
        Response.Write("<div id='error_paymentDateMonth' class='edit_error'>&laquo; OBS!<br></div>")
        Response.Write("<div id='error_paymentDateDay' class='edit_error'>&laquo; OBS!<br></div>")
        Response.Write("</td>")
        Response.Write("</tr>")
        If Len(invoiceID) > 0 Then 
            Response.Write("<tr><td align='right' colspan='2'><input type='submit' value='Save'></td></tr>")
        End IF
        Response.Write("</form>")
        Response.Write("<tr><td colspan='3'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("<td width='100'></td>")
        Response.Write("<td valign='top'>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("<tr><td><br></td></tr>")
        totInvoiceAmount = 0
        totInvoiceClaim = 0
        Response.Write("<tr>")
        Response.Write("<td valign='top' colspan='3'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='100%'>")
        Response.Write("<tr><td class='headline' colspan='14'>UNPAID INVOICES<hr class='hrHeadline'></td></tr>")
        Response.Write("<tr bgcolor='#eeeeee'>")
        Response.Write("<td class='formBold'>Invoice ID</td>")
        Response.Write("<td class='formBold'>References invoice ID</td>")
        Response.Write("<td class='formBold'>Partner</td>")
        Response.Write("<td class='formBold'>Contract number</td>")
        Response.Write("<td class='formBold'>Licence number</td>")
        Response.Write("<td class='formBold'>VIN-number</td>")
        Response.Write("<td class='formBold' align='right'>Invoice amount (Tax incl.)</td>")
        Response.Write("<td class='formBold' align='right'>Invoice claim (Tax incl.)</td>")
        Response.Write("<td class='formBold' align='right'>Invoice date</td>")
        Response.Write("<td class='formBold' align='right'>Expiry date</td>")
        Response.Write("<td class='formBold' align='right'>Days</td>")
        Response.Write("<td class='formBold' align='right'>Invoice printed</td>")
        Response.Write("<td class='formBold' align='right'>Country</td>")
        Response.Write("<td class='formBold' align='right'></td>")
        Response.Write("</tr>")
        dbOpen()
        RecSet = Conn.Execute("Select i.invoiceID, i.invoiceAmount, i.invoiceClaim, i.invoiceDate, i.expiryDate, i.invoicePrinted, p.partnerName, c.country, c.countryCurrency, c.decimals, ic.invoiceClaim As invoiceCreditAmount, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (((tblInvoice i INNER JOIN tblPartner p ON p.partnerID = i.partnerID) INNER JOIN tblCountry c ON c.countryID = i.countryID) LEFT JOIN tblInvoiceCredit ic ON ic.invoiceID = i.invoiceID) Where i.invoiceDate Is Not Null And i.invoiceSettled = 0 Order By i.invoiceID")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                invoiceID = RecSet.Fields("invoiceID").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                invoiceClaim = RecSet.Fields("invoiceClaim").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                expiryDate = RecSet.Fields("expiryDate").Value
                invoicePrinted = RecSet.Fields("invoicePrinted").Value
                partnerName = RecSet.Fields("partnerName").Value
                country = RecSet.Fields("country").Value
                countryCurrency = RecSet.Fields("countryCurrency").Value
                decimals = RecSet.Fields("decimals").Value
                invoiceCreditAmount = RecSet.Fields("invoiceCreditAmount").Value
                salesTax = RecSet.Fields("salesTax").Value
                salesTax = salesTax + 100
                salesTax = CDbl(salesTax) / 100
                If IsDBNull(invoiceCreditAmount) = false Then 
                    invoiceAmount = CDbl(invoiceAmount) - CDbl(invoiceCreditAmount)
                    invoiceClaim = CDbl(invoiceClaim) - CDbl(invoiceCreditAmount)
                End IF
                invoiceAmount = CDbl(invoiceAmount) * CDbl(salesTax)
                invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                invoiceAmount = CDbl(invoiceAmount)
                invoiceClaim = CDbl(invoiceClaim) * CDbl(salesTax)
                invoiceClaim = FormatNumber(CDbl(invoiceClaim), decimals)
                invoiceClaim = CDbl(invoiceClaim)
                totInvoiceAmount = totInvoiceAmount + invoiceAmount
                totInvoiceClaim = totInvoiceClaim + invoiceClaim
                expiryDays = DateDiff("d", Now(), Convert.ToDateTime(expiryDate))
                If expiryDays >= 0 Then 
                    expiryDays = "<font color='green'>" & expiryDays & "</font>"
                Else
                    expiryDays = "<font color='red'>" & expiryDays & "</font>"
                End IF
                If CInt(invoicePrinted) = 0 Then 
                    invoicePrinted = "<font color='red'>No</font>"
                Else
                    invoicePrinted = "<font color='green'>Yes</font>"
                End IF
                If rowNumber = 0 Then 
                    strGrey = " bgcolor='#eeeeee'"
                    rowNumber = 1
                Else
                    strGrey = ""
                    rowNumber = 0
                End IF
                Response.Write("<tr" & strGrey & ">")
                Response.Write("<td>" & invoiceID & "</td>")
                Response.Write("<td></td>")
                Response.Write("<td>" & partnerName & "</td>")
                Response.Write("<td>N/A</td>")
                Response.Write("<td>N/A</td>")
                Response.Write("<td>N/A</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(invoiceAmount), 2) & " " & countryCurrency & "</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(invoiceClaim), 2) & " " & countryCurrency & "</td>")
                Response.Write("<td align='right'>" & invoiceDate & "</td>")
                Response.Write("<td align='right'>" & expiryDate & "</td>")
                Response.Write("<td align='right'>" & expiryDays & "</td>")
                Response.Write("<td align='right'>" & invoicePrinted & "</td>")
                Response.Write("<td align='right'>" & country & "</td>")
                Response.Write("<td align='right'><a href='settleInvoice.aspx?invoiceID=" & invoiceID & "'>settle</a></td>")
                Response.Write("</tr>")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        RecSet = Conn.Execute("Select i.invoicePriceListModificationID As invoiceID, i.invoiceAmount, i.invoiceClaim, i.invoiceDate, i.expiryDate, i.invoicePrinted, p.partnerName, c.country, c.countryCurrency, c.decimals, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From ((tblInvoicePriceListModification i INNER JOIN tblPartner p ON p.partnerID = i.partnerID) INNER JOIN tblCountry c ON c.countryID = i.countryID) Where i.invoiceDate Is Not Null AND i.invoiceSettled = 0 Order By i.invoicePriceListModificationID")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                invoiceID = RecSet.Fields("invoiceID").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                invoiceClaim = RecSet.Fields("invoiceClaim").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                expiryDate = RecSet.Fields("expiryDate").Value
                invoicePrinted = RecSet.Fields("invoicePrinted").Value
                partnerName = RecSet.Fields("partnerName").Value
                country = RecSet.Fields("country").Value
                countryCurrency = RecSet.Fields("countryCurrency").Value
                decimals = RecSet.Fields("decimals").Value
                salesTax = RecSet.Fields("salesTax").Value
                salesTax = salesTax + 100
                salesTax = CDbl(salesTax) / 100
                invoiceAmount = CDbl(invoiceAmount) * CDbl(salesTax)
                invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                invoiceAmount = CDbl(invoiceAmount)
                invoiceClaim = CDbl(invoiceClaim) * CDbl(salesTax)
                invoiceClaim = FormatNumber(CDbl(invoiceClaim), decimals)
                invoiceClaim = CDbl(invoiceClaim)
                totInvoiceAmount = totInvoiceAmount + invoiceAmount
                totInvoiceClaim = totInvoiceClaim + invoiceClaim
                expiryDays = DateDiff("d", Now(), Convert.ToDateTime(expiryDate))
                If expiryDays >= 0 Then 
                    expiryDays = "<font color='green'>" & expiryDays & "</font>"
                Else
                    expiryDays = "<font color='red'>" & expiryDays & "</font>"
                End IF
                If CInt(invoicePrinted) = 0 Then 
                    invoicePrinted = "<font color='red'>No</font>"
                Else
                    invoicePrinted = "<font color='green'>Yes</font>"
                End IF
                If rowNumber = 0 Then 
                    strGrey = " bgcolor='#eeeeee'"
                    rowNumber = 1
                Else
                    strGrey = ""
                    rowNumber = 0
                End IF
                Response.Write("<tr" & strGrey & ">")
                Response.Write("<td>" & invoiceID & "</td>")
                Response.Write("<td></td>")
                Response.Write("<td>" & partnerName & "</td>")
                Response.Write("<td>N/A</td>")
                Response.Write("<td>N/A</td>")
                Response.Write("<td>N/A</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(invoiceAmount), 2) & " " & countryCurrency & "</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(invoiceClaim), 2) & " " & countryCurrency & "</td>")
                Response.Write("<td align='right'>" & invoiceDate & "</td>")
                Response.Write("<td align='right'>" & expiryDate & "</td>")
                Response.Write("<td align='right'>" & expiryDays & "</td>")
                Response.Write("<td align='right'>" & invoicePrinted & "</td>")
                Response.Write("<td align='right'>" & country & "</td>")
                Response.Write("<td align='right'><a href='settleInvoice.aspx?invoiceID=" & invoiceID & "'>settle</a></td>")
                Response.Write("</tr>")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        RecSet = Conn.Execute("Select i.invoiceDurationPenaltyID As invoiceID, i.invoiceAmount, i.invoiceClaim, i.invoiceDate, i.expiryDate, i.invoicePrinted, v.regnr, v.vinNumber, v.contractNumber, p.partnerName, c.country, c.countryCurrency, c.decimals, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (((tblInvoiceDurationPenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) INNER JOIN tblCountry c ON c.countryID = v.countryID) Where i.invoiceDate Is Not Null And i.invoiceSettled = 0 Order By i.invoiceDurationPenaltyID")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                invoiceID = RecSet.Fields("invoiceID").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                invoiceClaim = RecSet.Fields("invoiceClaim").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                expiryDate = RecSet.Fields("expiryDate").Value
                invoicePrinted = RecSet.Fields("invoicePrinted").Value
                regnr = RecSet.Fields("regnr").Value
                vinNumber = RecSet.Fields("vinNumber").Value
                contractNumber = RecSet.Fields("contractNumber").Value
                partnerName = RecSet.Fields("partnerName").Value
                country = RecSet.Fields("country").Value
                countryCurrency = RecSet.Fields("countryCurrency").Value
                decimals = RecSet.Fields("decimals").Value
                salesTax = RecSet.Fields("salesTax").Value
                salesTax = salesTax + 100
                salesTax = CDbl(salesTax) / 100
                invoiceAmount = CDbl(invoiceAmount) * CDbl(salesTax)
                invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                invoiceAmount = CDbl(invoiceAmount)
                invoiceClaim = CDbl(invoiceClaim) * CDbl(salesTax)
                invoiceClaim = FormatNumber(CDbl(invoiceClaim), decimals)
                invoiceClaim = CDbl(invoiceClaim)
                totInvoiceAmount = totInvoiceAmount + invoiceAmount
                totInvoiceClaim = totInvoiceClaim + invoiceClaim
                expiryDays = DateDiff("d", Now(), Convert.ToDateTime(expiryDate))
                If expiryDays >= 0 Then 
                    expiryDays = "<font color='green'>" & expiryDays & "</font>"
                Else
                    expiryDays = "<font color='red'>" & expiryDays & "</font>"
                End IF
                If CInt(invoicePrinted) = 0 Then 
                    invoicePrinted = "<font color='red'>No</font>"
                Else
                    invoicePrinted = "<font color='green'>Yes</font>"
                End IF
                If rowNumber = 0 Then 
                    strGrey = " bgcolor='#eeeeee'"
                    rowNumber = 1
                Else
                    strGrey = ""
                    rowNumber = 0
                End IF
                Response.Write("<tr" & strGrey & ">")
                Response.Write("<td>" & invoiceID & "</td>")
                Response.Write("<td></td>")
                Response.Write("<td>" & partnerName & "</td>")
                Response.Write("<td>" & contractNumber & "</td>")
                Response.Write("<td>" & regnr & "</td>")
                Response.Write("<td>" & vinNumber & "</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(invoiceAmount), 2) & " " & countryCurrency & "</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(invoiceClaim), 2) & " " & countryCurrency & "</td>")
                Response.Write("<td align='right'>" & invoiceDate & "</td>")
                Response.Write("<td align='right'>" & expiryDate & "</td>")
                Response.Write("<td align='right'>" & expiryDays & "</td>")
                Response.Write("<td align='right'>" & invoicePrinted & "</td>")
                Response.Write("<td align='right'>" & country & "</td>")
                Response.Write("<td align='right'><a href='settleInvoice.aspx?invoiceID=" & invoiceID & "'>settle</a></td>")
                Response.Write("</tr>")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        RecSet = Conn.Execute("Select i.invoiceCreditDurationPenaltyID As invoiceCreditID, i.invoiceDurationPenaltyID As invoiceID, i.invoiceAmount, i.invoiceClaim, i.invoiceDate, i.expiryDate, i.invoicePrinted, v.regnr, v.vinNumber, v.contractNumber, p.partnerName, c.country, c.countryCurrency, c.decimals, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (((tblInvoiceCreditDurationPenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) INNER JOIN tblCountry c ON c.countryID = v.countryID) Where i.invoiceDate Is Not Null And i.invoiceSettled = 0 Order By i.invoiceCreditDurationPenaltyID")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                invoiceCreditID = RecSet.Fields("invoiceCreditID").Value
                invoiceID = RecSet.Fields("invoiceID").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                invoiceClaim = RecSet.Fields("invoiceClaim").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                expiryDate = RecSet.Fields("expiryDate").Value
                invoicePrinted = RecSet.Fields("invoicePrinted").Value
                regnr = RecSet.Fields("regnr").Value
                vinNumber = RecSet.Fields("vinNumber").Value
                contractNumber = RecSet.Fields("contractNumber").Value
                partnerName = RecSet.Fields("partnerName").Value
                country = RecSet.Fields("country").Value
                countryCurrency = RecSet.Fields("countryCurrency").Value
                decimals = RecSet.Fields("decimals").Value
                salesTax = RecSet.Fields("salesTax").Value
                salesTax = salesTax + 100
                salesTax = CDbl(salesTax) / 100
                invoiceAmount = CDbl(invoiceAmount) * CDbl(salesTax)
                invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                invoiceAmount = CDbl(invoiceAmount)
                invoiceAmount = CDbl(invoiceAmount) * - 1
                invoiceClaim = CDbl(invoiceClaim) * CDbl(salesTax)
                invoiceClaim = FormatNumber(CDbl(invoiceClaim), decimals)
                invoiceClaim = CDbl(invoiceClaim)
                invoiceClaim = CDbl(invoiceClaim) * - 1
                totInvoiceAmount = totInvoiceAmount + invoiceAmount
                totInvoiceClaim = totInvoiceClaim + invoiceClaim
                expiryDays = DateDiff("d", Now(), Convert.ToDateTime(expiryDate))
                If expiryDays >= 0 Then 
                    expiryDays = "<font color='green'>" & expiryDays & "</font>"
                Else
                    expiryDays = "<font color='red'>" & expiryDays & "</font>"
                End IF
                If CInt(invoicePrinted) = 0 Then 
                    invoicePrinted = "<font color='red'>No</font>"
                Else
                    invoicePrinted = "<font color='green'>Yes</font>"
                End IF
                If rowNumber = 0 Then 
                    strGrey = " bgcolor='#eeeeee'"
                    rowNumber = 1
                Else
                    strGrey = ""
                    rowNumber = 0
                End IF
                Response.Write("<tr" & strGrey & ">")
                Response.Write("<td>" & invoiceCreditID & "</td>")
                Response.Write("<td>" & invoiceID & "</td>")
                Response.Write("<td>" & partnerName & "</td>")
                Response.Write("<td>" & contractNumber & "</td>")
                Response.Write("<td>" & regnr & "</td>")
                Response.Write("<td>" & vinNumber & "</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(invoiceAmount), 2) & " " & countryCurrency & "</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(invoiceClaim), 2) & " " & countryCurrency & "</td>")
                Response.Write("<td align='right'>" & invoiceDate & "</td>")
                Response.Write("<td align='right'>" & expiryDate & "</td>")
                Response.Write("<td align='right'>" & expiryDays & "</td>")
                Response.Write("<td align='right'>" & invoicePrinted & "</td>")
                Response.Write("<td align='right'>" & country & "</td>")
                Response.Write("<td align='right'><a href='settleInvoice.aspx?invoiceID=" & invoiceCreditID & "'>settle</a></td>")
                Response.Write("</tr>")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        RecSet = Conn.Execute("Select i.invoiceDistancePenaltyID As invoiceID, i.invoiceAmount, i.invoiceClaim, i.invoiceDate, i.expiryDate, i.invoicePrinted, v.regnr, v.vinNumber, v.contractNumber, p.partnerName, c.country, c.countryCurrency, c.decimals, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (((tblInvoiceDistancePenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) INNER JOIN tblCountry c ON c.countryID = v.countryID) Where i.invoiceDate Is Not Null And i.invoiceSettled = 0 Order By i.invoiceDistancePenaltyID")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                invoiceID = RecSet.Fields("invoiceID").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                invoiceClaim = RecSet.Fields("invoiceClaim").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                expiryDate = RecSet.Fields("expiryDate").Value
                invoicePrinted = RecSet.Fields("invoicePrinted").Value
                regnr = RecSet.Fields("regnr").Value
                vinNumber = RecSet.Fields("vinNumber").Value
                contractNumber = RecSet.Fields("contractNumber").Value
                partnerName = RecSet.Fields("partnerName").Value
                country = RecSet.Fields("country").Value
                countryCurrency = RecSet.Fields("countryCurrency").Value
                decimals = RecSet.Fields("decimals").Value
                salesTax = RecSet.Fields("salesTax").Value
                salesTax = salesTax + 100
                salesTax = CDbl(salesTax) / 100
                invoiceAmount = CDbl(invoiceAmount) * CDbl(salesTax)
                invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                invoiceAmount = CDbl(invoiceAmount)
                invoiceClaim = CDbl(invoiceClaim) * CDbl(salesTax)
                invoiceClaim = FormatNumber(CDbl(invoiceClaim), decimals)
                invoiceClaim = CDbl(invoiceClaim)
                totInvoiceAmount = totInvoiceAmount + invoiceAmount
                totInvoiceClaim = totInvoiceClaim + invoiceClaim
                expiryDays = DateDiff("d", Now(), Convert.ToDateTime(expiryDate))
                If expiryDays >= 0 Then 
                    expiryDays = "<font color='green'>" & expiryDays & "</font>"
                Else
                    expiryDays = "<font color='red'>" & expiryDays & "</font>"
                End IF
                If CInt(invoicePrinted) = 0 Then 
                    invoicePrinted = "<font color='red'>No</font>"
                Else
                    invoicePrinted = "<font color='green'>Yes</font>"
                End IF
                If rowNumber = 0 Then 
                    strGrey = " bgcolor='#eeeeee'"
                    rowNumber = 1
                Else
                    strGrey = ""
                    rowNumber = 0
                End IF
                Response.Write("<tr" & strGrey & ">")
                Response.Write("<td>" & invoiceID & "</td>")
                Response.Write("<td></td>")
                Response.Write("<td>" & partnerName & "</td>")
                Response.Write("<td>" & contractNumber & "</td>")
                Response.Write("<td>" & regnr & "</td>")
                Response.Write("<td>" & vinNumber & "</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(invoiceAmount), 2) & " " & countryCurrency & "</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(invoiceClaim), 2) & " " & countryCurrency & "</td>")
                Response.Write("<td align='right'>" & invoiceDate & "</td>")
                Response.Write("<td align='right'>" & expiryDate & "</td>")
                Response.Write("<td align='right'>" & expiryDays & "</td>")
                Response.Write("<td align='right'>" & invoicePrinted & "</td>")
                Response.Write("<td align='right'>" & country & "</td>")
                Response.Write("<td align='right'><a href='settleInvoice.aspx?invoiceID=" & invoiceID & "'>settle</a></td>")
                Response.Write("</tr>")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        RecSet = Conn.Execute("Select i.invoiceCreditDistancePenaltyID As invoiceCreditID, i.invoiceDistancePenaltyID As invoiceID, i.invoiceAmount, i.invoiceClaim, i.invoiceDate, i.expiryDate, i.invoicePrinted, v.regnr, v.vinNumber, v.contractNumber, p.partnerName, c.country, c.countryCurrency, c.decimals, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (((tblInvoiceCreditDistancePenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) INNER JOIN tblCountry c ON c.countryID = v.countryID) Where i.invoiceDate Is Not Null And i.invoiceSettled = 0 Order By i.invoiceCreditDistancePenaltyID")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                invoiceCreditID = RecSet.Fields("invoiceCreditID").Value
                invoiceID = RecSet.Fields("invoiceID").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                invoiceClaim = RecSet.Fields("invoiceClaim").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                expiryDate = RecSet.Fields("expiryDate").Value
                invoicePrinted = RecSet.Fields("invoicePrinted").Value
                regnr = RecSet.Fields("regnr").Value
                vinNumber = RecSet.Fields("vinNumber").Value
                contractNumber = RecSet.Fields("contractNumber").Value
                partnerName = RecSet.Fields("partnerName").Value
                country = RecSet.Fields("country").Value
                countryCurrency = RecSet.Fields("countryCurrency").Value
                decimals = RecSet.Fields("decimals").Value
                salesTax = RecSet.Fields("salesTax").Value
                salesTax = salesTax + 100
                salesTax = CDbl(salesTax) / 100
                invoiceAmount = CDbl(invoiceAmount) * CDbl(salesTax)
                invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                invoiceAmount = CDbl(invoiceAmount)
                invoiceAmount = CDbl(invoiceAmount) * - 1
                invoiceClaim = CDbl(invoiceClaim) * CDbl(salesTax)
                invoiceClaim = FormatNumber(CDbl(invoiceClaim), decimals)
                invoiceClaim = CDbl(invoiceClaim)
                invoiceClaim = CDbl(invoiceClaim) * - 1
                totInvoiceAmount = totInvoiceAmount + invoiceAmount
                totInvoiceClaim = totInvoiceClaim + invoiceClaim
                expiryDays = DateDiff("d", Now(), Convert.ToDateTime(expiryDate))
                If expiryDays >= 0 Then 
                    expiryDays = "<font color='green'>" & expiryDays & "</font>"
                Else
                    expiryDays = "<font color='red'>" & expiryDays & "</font>"
                End IF
                If CInt(invoicePrinted) = 0 Then 
                    invoicePrinted = "<font color='red'>No</font>"
                Else
                    invoicePrinted = "<font color='green'>Yes</font>"
                End IF
                If rowNumber = 0 Then 
                    strGrey = " bgcolor='#eeeeee'"
                    rowNumber = 1
                Else
                    strGrey = ""
                    rowNumber = 0
                End IF
                Response.Write("<tr" & strGrey & ">")
                Response.Write("<td>" & invoiceCreditID & "</td>")
                Response.Write("<td>" & invoiceID & "</td>")
                Response.Write("<td>" & partnerName & "</td>")
                Response.Write("<td>" & contractNumber & "</td>")
                Response.Write("<td>" & regnr & "</td>")
                Response.Write("<td>" & vinNumber & "</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(invoiceAmount), 2) & " " & countryCurrency & "</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(invoiceClaim), 2) & " " & countryCurrency & "</td>")
                Response.Write("<td align='right'>" & invoiceDate & "</td>")
                Response.Write("<td align='right'>" & expiryDate & "</td>")
                Response.Write("<td align='right'>" & expiryDays & "</td>")
                Response.Write("<td align='right'>" & invoicePrinted & "</td>")
                Response.Write("<td align='right'>" & country & "</td>")
                Response.Write("<td align='right'><a href='settleInvoice.aspx?invoiceID=" & invoiceCreditID & "'>settle</a></td>")
                Response.Write("</tr>")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        RecSet = Conn.Execute("Select i.invoiceAdminFeeID As invoiceID, i.invoiceAmount, i.invoiceClaim, i.invoiceDate, i.expiryDate, i.invoicePrinted, p.partnerName, c.country, c.countryCurrency, c.decimals, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From ((tblInvoiceAdminFee i INNER JOIN tblPartner p ON p.partnerID = i.partnerID) INNER JOIN tblCountry c ON c.countryID = i.countryID) Where i.invoiceDate Is Not Null And i.invoiceSettled = 0 Order By i.invoiceAdminFeeID")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                invoiceID = RecSet.Fields("invoiceID").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                invoiceClaim = RecSet.Fields("invoiceClaim").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                expiryDate = RecSet.Fields("expiryDate").Value
                invoicePrinted = RecSet.Fields("invoicePrinted").Value
                partnerName = RecSet.Fields("partnerName").Value
                country = RecSet.Fields("country").Value
                countryCurrency = RecSet.Fields("countryCurrency").Value
                decimals = RecSet.Fields("decimals").Value
                salesTax = RecSet.Fields("salesTax").Value
                salesTax = salesTax + 100
                salesTax = CDbl(salesTax) / 100
                invoiceAmount = CDbl(invoiceAmount) * CDbl(salesTax)
                invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                invoiceAmount = CDbl(invoiceAmount)
                invoiceClaim = CDbl(invoiceClaim) * CDbl(salesTax)
                invoiceClaim = FormatNumber(CDbl(invoiceClaim), decimals)
                invoiceClaim = CDbl(invoiceClaim)
                totInvoiceAmount = totInvoiceAmount + invoiceAmount
                totInvoiceClaim = totInvoiceClaim + invoiceClaim
                expiryDays = DateDiff("d", Now(), Convert.ToDateTime(expiryDate))
                If expiryDays >= 0 Then 
                    expiryDays = "<font color='green'>" & expiryDays & "</font>"
                Else
                    expiryDays = "<font color='red'>" & expiryDays & "</font>"
                End IF
                If CInt(invoicePrinted) = 0 Then 
                    invoicePrinted = "<font color='red'>No</font>"
                Else
                    invoicePrinted = "<font color='green'>Yes</font>"
                End IF
                If rowNumber = 0 Then 
                    strGrey = " bgcolor='#eeeeee'"
                    rowNumber = 1
                Else
                    strGrey = ""
                    rowNumber = 0
                End IF
                Response.Write("<tr" & strGrey & ">")
                Response.Write("<td>" & invoiceID & "</td>")
                Response.Write("<td></td>")
                Response.Write("<td>" & partnerName & "</td>")
                Response.Write("<td>N/A</td>")
                Response.Write("<td>N/A</td>")
                Response.Write("<td>N/A</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(invoiceAmount), 2) & " " & countryCurrency & "</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(invoiceClaim), 2) & " " & countryCurrency & "</td>")
                Response.Write("<td align='right'>" & invoiceDate & "</td>")
                Response.Write("<td align='right'>" & expiryDate & "</td>")
                Response.Write("<td align='right'>" & expiryDays & "</td>")
                Response.Write("<td align='right'>" & invoicePrinted & "</td>")
                Response.Write("<td align='right'>" & country & "</td>")
                Response.Write("<td align='right'><a href='settleInvoice.aspx?invoiceID=" & invoiceID & "'>settle</a></td>")
                Response.Write("</tr>")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        RecSet = Conn.Execute("Select i.invoiceServiceAccountBalanceID As invoiceID, i.invoiceAmount, i.invoiceClaim, i.invoiceDate, i.expiryDate, i.invoicePrinted, v.regnr, v.vinNumber, v.contractNumber, p.partnerName, c.country, c.countryCurrency, c.decimals, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (((tblInvoiceServiceAccountBalance i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) INNER JOIN tblCountry c ON c.countryID = v.countryID) Where i.invoiceDate Is Not Null And i.invoiceSettled = 0 Order By i.invoiceServiceAccountBalanceID")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                invoiceID = RecSet.Fields("invoiceID").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                invoiceClaim = RecSet.Fields("invoiceClaim").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                expiryDate = RecSet.Fields("expiryDate").Value
                invoicePrinted = RecSet.Fields("invoicePrinted").Value
                regnr = RecSet.Fields("regnr").Value
                vinNumber = RecSet.Fields("vinNumber").Value
                contractNumber = RecSet.Fields("contractNumber").Value
                partnerName = RecSet.Fields("partnerName").Value
                country = RecSet.Fields("country").Value
                countryCurrency = RecSet.Fields("countryCurrency").Value
                decimals = RecSet.Fields("decimals").Value
                salesTax = RecSet.Fields("salesTax").Value
                salesTax = salesTax + 100
                salesTax = CDbl(salesTax) / 100
                invoiceAmount = CDbl(invoiceAmount) * CDbl(salesTax)
                invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                invoiceAmount = CDbl(invoiceAmount)
                invoiceClaim = CDbl(invoiceClaim) * CDbl(salesTax)
                invoiceClaim = FormatNumber(CDbl(invoiceClaim), decimals)
                invoiceClaim = CDbl(invoiceClaim)
                totInvoiceAmount = totInvoiceAmount + invoiceAmount
                totInvoiceClaim = totInvoiceClaim + invoiceClaim
                expiryDays = DateDiff("d", Now(), Convert.ToDateTime(expiryDate))
                If expiryDays >= 0 Then 
                    expiryDays = "<font color='green'>" & expiryDays & "</font>"
                Else
                    expiryDays = "<font color='red'>" & expiryDays & "</font>"
                End IF
                If CInt(invoicePrinted) = 0 Then 
                    invoicePrinted = "<font color='red'>No</font>"
                Else
                    invoicePrinted = "<font color='green'>Yes</font>"
                End IF
                If rowNumber = 0 Then 
                    strGrey = " bgcolor='#eeeeee'"
                    rowNumber = 1
                Else
                    strGrey = ""
                    rowNumber = 0
                End IF
                Response.Write("<tr" & strGrey & ">")
                Response.Write("<td>" & invoiceID & "</td>")
                Response.Write("<td></td>")
                Response.Write("<td>" & partnerName & "</td>")
                Response.Write("<td>" & contractNumber & "</td>")
                Response.Write("<td>" & regnr & "</td>")
                Response.Write("<td>" & vinNumber & "</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(invoiceAmount), 2) & " " & countryCurrency & "</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(invoiceClaim), 2) & " " & countryCurrency & "</td>")
                Response.Write("<td align='right'>" & invoiceDate & "</td>")
                Response.Write("<td align='right'>" & expiryDate & "</td>")
                Response.Write("<td align='right'>" & expiryDays & "</td>")
                Response.Write("<td align='right'>" & invoicePrinted & "</td>")
                Response.Write("<td align='right'>" & country & "</td>")
                Response.Write("<td align='right'><a href='settleInvoice.aspx?invoiceID=" & invoiceID & "'>settle</a></td>")
                Response.Write("</tr>")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        RecSet = Conn.Execute("Select i.invoiceCreditServiceAccountBalanceID As invoiceCreditID, i.invoiceServiceAccountBalanceID As invoiceID, i.invoiceAmount, i.invoiceClaim, i.invoiceDate, i.expiryDate, i.invoicePrinted, v.regnr, v.vinNumber, v.contractNumber, p.partnerName, c.country, c.countryCurrency, c.decimals, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (((tblInvoiceCreditServiceAccountBalance i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) INNER JOIN tblCountry c ON c.countryID = v.countryID) Where i.invoiceDate Is Not Null And i.invoiceSettled = 0 Order By i.invoiceCreditServiceAccountBalanceID")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                invoiceCreditID = RecSet.Fields("invoiceCreditID").Value
                invoiceID = RecSet.Fields("invoiceID").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                invoiceClaim = RecSet.Fields("invoiceClaim").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                expiryDate = RecSet.Fields("expiryDate").Value
                invoicePrinted = RecSet.Fields("invoicePrinted").Value
                regnr = RecSet.Fields("regnr").Value
                vinNumber = RecSet.Fields("vinNumber").Value
                contractNumber = RecSet.Fields("contractNumber").Value
                partnerName = RecSet.Fields("partnerName").Value
                country = RecSet.Fields("country").Value
                countryCurrency = RecSet.Fields("countryCurrency").Value
                decimals = RecSet.Fields("decimals").Value
                salesTax = RecSet.Fields("salesTax").Value
                salesTax = salesTax + 100
                salesTax = CDbl(salesTax) / 100
                invoiceAmount = CDbl(invoiceAmount) * CDbl(salesTax)
                invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                invoiceAmount = CDbl(invoiceAmount)
                invoiceClaim = CDbl(invoiceClaim) * CDbl(salesTax)
                invoiceClaim = FormatNumber(CDbl(invoiceClaim), decimals)
                invoiceClaim = CDbl(invoiceClaim)
                totInvoiceAmount = totInvoiceAmount + invoiceAmount
                totInvoiceClaim = totInvoiceClaim + invoiceClaim
                expiryDays = DateDiff("d", Now(), Convert.ToDateTime(expiryDate))
                If expiryDays >= 0 Then 
                    expiryDays = "<font color='green'>" & expiryDays & "</font>"
                Else
                    expiryDays = "<font color='red'>" & expiryDays & "</font>"
                End IF
                If CInt(invoicePrinted) = 0 Then 
                    invoicePrinted = "<font color='red'>No</font>"
                Else
                    invoicePrinted = "<font color='green'>Yes</font>"
                End IF
                If rowNumber = 0 Then 
                    strGrey = " bgcolor='#eeeeee'"
                    rowNumber = 1
                Else
                    strGrey = ""
                    rowNumber = 0
                End IF
                Response.Write("<tr" & strGrey & ">")
                Response.Write("<td>" & invoiceCreditID & "</td>")
                Response.Write("<td>" & invoiceID & "</td>")
                Response.Write("<td>" & partnerName & "</td>")
                Response.Write("<td>" & contractNumber & "</td>")
                Response.Write("<td>" & regnr & "</td>")
                Response.Write("<td>" & vinNumber & "</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(invoiceAmount), 2) & " " & countryCurrency & "</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(invoiceClaim), 2) & " " & countryCurrency & "</td>")
                Response.Write("<td align='right'>" & invoiceDate & "</td>")
                Response.Write("<td align='right'>" & expiryDate & "</td>")
                Response.Write("<td align='right'>" & expiryDays & "</td>")
                Response.Write("<td align='right'>" & invoicePrinted & "</td>")
                Response.Write("<td align='right'>" & country & "</td>")
                Response.Write("<td align='right'><a href='settleInvoice.aspx?invoiceID=" & invoiceCreditID & "'>settle</a></td>")
                Response.Write("</tr>")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        RecSet = Conn.Execute("Select i.invoiceSeparateID As invoiceID, i.invoiceAmount, i.invoiceClaim, i.invoiceDate, i.expiryDate, i.invoicePrinted, p.partnerName, c.country, c.countryCurrency, c.decimals, v.regnr, v.vinNumber, v.contractNumber, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (((tblInvoiceSeparate i INNER JOIN tblPartner p ON p.partnerID = i.partnerID) INNER JOIN tblCountry c ON c.countryID = i.countryID) LEFT JOIN tblVehicle v ON v.vehicleID = i.vehicleID) Where i.invoiceDate Is Not Null And i.invoiceSettled = 0 Order By i.invoiceSeparateID")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                invoiceID = RecSet.Fields("invoiceID").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                invoiceClaim = RecSet.Fields("invoiceClaim").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                expiryDate = RecSet.Fields("expiryDate").Value
                invoicePrinted = RecSet.Fields("invoicePrinted").Value
                partnerName = RecSet.Fields("partnerName").Value
                country = RecSet.Fields("country").Value
                countryCurrency = RecSet.Fields("countryCurrency").Value
                decimals = RecSet.Fields("decimals").Value
                regnr = RecSet.Fields("regnr").Value
                vinNumber = RecSet.Fields("vinNumber").Value
                contractNumber = RecSet.Fields("contractNumber").Value
                salesTax = RecSet.Fields("salesTax").Value
                salesTax = salesTax + 100
                salesTax = CDbl(salesTax) / 100
                invoiceAmount = CDbl(invoiceAmount) * CDbl(salesTax)
                invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                invoiceAmount = CDbl(invoiceAmount)
                invoiceClaim = CDbl(invoiceClaim) * CDbl(salesTax)
                invoiceClaim = FormatNumber(CDbl(invoiceClaim), decimals)
                invoiceClaim = CDbl(invoiceClaim)
                totInvoiceAmount = totInvoiceAmount + invoiceAmount
                totInvoiceClaim = totInvoiceClaim + invoiceClaim
                expiryDays = DateDiff("d", Now(), Convert.ToDateTime(expiryDate))
                If expiryDays >= 0 Then 
                    expiryDays = "<font color='green'>" & expiryDays & "</font>"
                Else
                    expiryDays = "<font color='red'>" & expiryDays & "</font>"
                End IF
                If CInt(invoicePrinted) = 0 Then 
                    invoicePrinted = "<font color='red'>No</font>"
                Else
                    invoicePrinted = "<font color='green'>Yes</font>"
                End IF
                If IsDBNull(regnr) = true Then 
                    regnr = "N/A"
                End IF
                If IsDBNull(vinNumber) = true Then 
                    vinNumber = "N/A"
                End IF
                If IsDBNull(contractNumber) = true Then 
                    contractNumber = "N/A"
                End IF
                If rowNumber = 0 Then 
                    strGrey = " bgcolor='#eeeeee'"
                    rowNumber = 1
                Else
                    strGrey = ""
                    rowNumber = 0
                End IF
                Response.Write("<tr" & strGrey & ">")
                Response.Write("<td>" & invoiceID & "</td>")
                Response.Write("<td></td>")
                Response.Write("<td>" & partnerName & "</td>")
                Response.Write("<td>" & contractNumber & "</td>")
                Response.Write("<td>" & regnr & "</td>")
                Response.Write("<td>" & vinNumber & "</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(invoiceAmount), 2) & " " & countryCurrency & "</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(invoiceClaim), 2) & " " & countryCurrency & "</td>")
                Response.Write("<td align='right'>" & invoiceDate & "</td>")
                Response.Write("<td align='right'>" & expiryDate & "</td>")
                Response.Write("<td align='right'>" & expiryDays & "</td>")
                Response.Write("<td align='right'>" & invoicePrinted & "</td>")
                Response.Write("<td align='right'>" & country & "</td>")
                Response.Write("<td align='right'><a href='settleInvoice.aspx?invoiceID=" & invoiceID & "'>settle</a></td>")
                Response.Write("</tr>")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        RecSet = Conn.Execute("Select i.invoiceCreditSeparateID As invoiceCreditID, i.invoiceSeparateID As invoiceID, i.invoiceAmount, i.invoiceClaim, i.invoiceDate, i.expiryDate, i.invoicePrinted, p.partnerName, c.country, c.countryCurrency, c.decimals, v.regnr, v.vinNumber, v.contractNumber, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (((tblInvoiceCreditSeparate i INNER JOIN tblPartner p ON p.partnerID = i.partnerID) INNER JOIN tblCountry c ON c.countryID = i.countryID) LEFT JOIN tblVehicle v ON v.vehicleID = i.vehicleID) Where i.invoiceDate Is Not Null And i.invoiceSettled = 0 Order By i.invoiceCreditSeparateID")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                invoiceCreditID = RecSet.Fields("invoiceCreditID").Value
                invoiceID = RecSet.Fields("invoiceID").Value
                invoiceAmount = RecSet.Fields("invoiceAmount").Value
                invoiceClaim = RecSet.Fields("invoiceClaim").Value
                invoiceDate = RecSet.Fields("invoiceDate").Value
                expiryDate = RecSet.Fields("expiryDate").Value
                invoicePrinted = RecSet.Fields("invoicePrinted").Value
                partnerName = RecSet.Fields("partnerName").Value
                country = RecSet.Fields("country").Value
                countryCurrency = RecSet.Fields("countryCurrency").Value
                decimals = RecSet.Fields("decimals").Value
                regnr = RecSet.Fields("regnr").Value
                vinNumber = RecSet.Fields("vinNumber").Value
                contractNumber = RecSet.Fields("contractNumber").Value
                salesTax = RecSet.Fields("salesTax").Value
                salesTax = salesTax + 100
                salesTax = CDbl(salesTax) / 100
                invoiceAmount = CDbl(invoiceAmount) * CDbl(salesTax)
                invoiceAmount = FormatNumber(CDbl(invoiceAmount), decimals)
                invoiceAmount = CDbl(invoiceAmount)
                invoiceClaim = CDbl(invoiceClaim) * CDbl(salesTax)
                invoiceClaim = FormatNumber(CDbl(invoiceClaim), decimals)
                invoiceClaim = CDbl(invoiceClaim)
                totInvoiceAmount = totInvoiceAmount + invoiceAmount
                totInvoiceClaim = totInvoiceClaim + invoiceClaim
                expiryDays = DateDiff("d", Now(), Convert.ToDateTime(expiryDate))
                If expiryDays >= 0 Then 
                    expiryDays = "<font color='green'>" & expiryDays & "</font>"
                Else
                    expiryDays = "<font color='red'>" & expiryDays & "</font>"
                End IF
                If CInt(invoicePrinted) = 0 Then 
                    invoicePrinted = "<font color='red'>No</font>"
                Else
                    invoicePrinted = "<font color='green'>Yes</font>"
                End IF
                If IsDBNull(regnr) = true Then 
                    regnr = "N/A"
                End IF
                If IsDBNull(vinNumber) = true Then 
                    vinNumber = "N/A"
                End IF
                If IsDBNull(contractNumber) = true Then 
                    contractNumber = "N/A"
                End IF
                If rowNumber = 0 Then 
                    strGrey = " bgcolor='#eeeeee'"
                    rowNumber = 1
                Else
                    strGrey = ""
                    rowNumber = 0
                End IF
                Response.Write("<tr" & strGrey & ">")
                Response.Write("<td>" & invoiceCreditID & "</td>")
                Response.Write("<td>" & invoiceID & "</td>")
                Response.Write("<td>" & partnerName & "</td>")
                Response.Write("<td>" & contractNumber & "</td>")
                Response.Write("<td>" & regnr & "</td>")
                Response.Write("<td>" & vinNumber & "</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(invoiceAmount), 2) & " " & countryCurrency & "</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(invoiceClaim), 2) & " " & countryCurrency & "</td>")
                Response.Write("<td align='right'>" & invoiceDate & "</td>")
                Response.Write("<td align='right'>" & expiryDate & "</td>")
                Response.Write("<td align='right'>" & expiryDays & "</td>")
                Response.Write("<td align='right'>" & invoicePrinted & "</td>")
                Response.Write("<td align='right'>" & country & "</td>")
                Response.Write("<td align='right'><a href='settleInvoice.aspx?invoiceID=" & invoiceCreditID & "'>settle</a></td>")
                Response.Write("</tr>")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If rowNumber = 0 Then 
            strGrey = " bgcolor='#eeeeee'"
            rowNumber = 1
        Else
            strGrey = ""
            rowNumber = 0
        End IF
        Response.Write("<tr" & strGrey & ">")
        Response.Write("<td class='formBold'>Total sum:</td>")
        Response.Write("<td class='formBold' colspan='5'></td>")
        Response.Write("<td class='formBold' align='right'>" & FormatNumber(CDbl(totInvoiceAmount), 2) & " EUR</td>")
        Response.Write("<td class='formBold' align='right'>" & FormatNumber(CDbl(totInvoiceClaim), 2) & " EUR</td>")
        Response.Write("<td class='formBold' colspan='6'></td>")
        Response.Write("</tr>")
        Response.Write("<tr><td colspan='14'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</table>")
        Under()
    ElseIf Request.QueryString("do") = "save" Then 
        invoiceID = Request.QueryString("invoiceID")
        paymentAmount = Request.Form("paymentAmount")
        paymentDateYear = Request.Form("paymentDateYear")
        paymentDateMonth = Request.Form("paymentDateMonth")
        paymentDateDay = Request.Form("paymentDateDay")
        invoiceID = CLng(invoiceID)
        invoiceCreditAmount = 0
        dbOpen()
        RecSet = Conn.Execute("Select i.invoiceClaim, c.decimals, ic.invoiceClaim As invoiceCreditAmount, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (((tblInvoice i INNER JOIN tblPartner p ON p.partnerID = i.partnerID) INNER JOIN tblCountry c ON c.countryID = i.countryID) LEFT JOIN tblInvoiceCredit ic ON ic.invoiceID = i.invoiceID) Where i.invoiceID = " & invoiceID & "")
        If Not RecSet.EOF Then 
            invoiceClaim = RecSet.Fields("invoiceClaim").Value
            decimals = RecSet.Fields("decimals").Value
            invoiceCreditAmount = RecSet.Fields("invoiceCreditAmount").Value
            salesTax = RecSet.Fields("salesTax").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        RecSet = Conn.Execute("Select i.invoiceClaim, c.decimals, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From ((tblInvoicePriceListModification i INNER JOIN tblPartner p ON p.partnerID = i.partnerID) INNER JOIN tblCountry c ON c.countryID = i.countryID) Where invoicePriceListModificationID = " & invoiceID & "")
        If Not RecSet.EOF Then 
            invoiceClaim = RecSet.Fields("invoiceClaim").Value
            decimals = RecSet.Fields("decimals").Value
            salesTax = RecSet.Fields("salesTax").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        RecSet = Conn.Execute("Select i.invoiceClaim, c.decimals, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (((tblInvoiceDurationPenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) INNER JOIN tblCountry c ON c.countryID = v.countryID) Where invoiceDurationPenaltyID = " & invoiceID & "")
        If Not RecSet.EOF Then 
            invoiceClaim = RecSet.Fields("invoiceClaim").Value
            decimals = RecSet.Fields("decimals").Value
            salesTax = RecSet.Fields("salesTax").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        RecSet = Conn.Execute("Select i.invoiceClaim, c.decimals, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (((tblInvoiceCreditDurationPenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) INNER JOIN tblCountry c ON c.countryID = v.countryID) Where invoiceCreditDurationPenaltyID = " & invoiceID & "")
        If Not RecSet.EOF Then 
            invoiceClaim = RecSet.Fields("invoiceClaim").Value
            decimals = RecSet.Fields("decimals").Value
            salesTax = RecSet.Fields("salesTax").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        RecSet = Conn.Execute("Select i.invoiceClaim, c.decimals, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (((tblInvoiceDistancePenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) INNER JOIN tblCountry c ON c.countryID = v.countryID) Where invoiceDistancePenaltyID = " & invoiceID & "")
        If Not RecSet.EOF Then 
            invoiceClaim = RecSet.Fields("invoiceClaim").Value
            decimals = RecSet.Fields("decimals").Value
            salesTax = RecSet.Fields("salesTax").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        RecSet = Conn.Execute("Select i.invoiceClaim, c.decimals, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (((tblInvoiceCreditDistancePenalty i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) INNER JOIN tblCountry c ON c.countryID = v.countryID) Where invoiceCreditDistancePenaltyID = " & invoiceID & "")
        If Not RecSet.EOF Then 
            invoiceClaim = RecSet.Fields("invoiceClaim").Value
            decimals = RecSet.Fields("decimals").Value
            salesTax = RecSet.Fields("salesTax").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        RecSet = Conn.Execute("Select i.invoiceClaim, c.decimals, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From ((tblInvoiceAdminFee i INNER JOIN tblPartner p ON p.partnerID = i.partnerID) INNER JOIN tblCountry c ON c.countryID = i.countryID) Where invoiceAdminFeeID = " & invoiceID & "")
        If Not RecSet.EOF Then 
            invoiceClaim = RecSet.Fields("invoiceClaim").Value
            decimals = RecSet.Fields("decimals").Value
            salesTax = RecSet.Fields("salesTax").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        RecSet = Conn.Execute("Select i.invoiceClaim, c.decimals, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (((tblInvoiceServiceAccountBalance i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) INNER JOIN tblCountry c ON c.countryID = v.countryID) Where invoiceServiceAccountBalanceID = " & invoiceID & "")
        If Not RecSet.EOF Then 
            invoiceClaim = RecSet.Fields("invoiceClaim").Value
            decimals = RecSet.Fields("decimals").Value
            salesTax = RecSet.Fields("salesTax").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        RecSet = Conn.Execute("Select i.invoiceClaim, c.decimals, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (((tblInvoiceCreditServiceAccountBalance i INNER JOIN tblVehicle v ON v.vehicleID = i.vehicleID) INNER JOIN tblPartner p ON p.partnerID = v.partnerID) INNER JOIN tblCountry c ON c.countryID = v.countryID) Where invoiceCreditServiceAccountBalanceID = " & invoiceID & "")
        If Not RecSet.EOF Then 
            invoiceClaim = RecSet.Fields("invoiceClaim").Value
            decimals = RecSet.Fields("decimals").Value
            salesTax = RecSet.Fields("salesTax").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        RecSet = Conn.Execute("Select i.invoiceClaim, c.decimals, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (tblInvoiceSeparate i INNER JOIN tblCountry c ON c.countryID = i.countryID) Where invoiceSeparateID = " & invoiceID & "")
        If Not RecSet.EOF Then 
            invoiceClaim = RecSet.Fields("invoiceClaim").Value
            decimals = RecSet.Fields("decimals").Value
            salesTax = RecSet.Fields("salesTax").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        RecSet = Conn.Execute("Select i.invoiceClaim, c.decimals, (Select TOP 1 salesTax From tblSalesTax st Where st.countryID = c.countryID And st.date <= i.invoiceDate Order By st.date desc) As salesTax From (tblInvoiceCreditSeparate i INNER JOIN tblCountry c ON c.countryID = i.countryID) Where invoiceCreditSeparateID = " & invoiceID & "")
        If Not RecSet.EOF Then 
            invoiceClaim = RecSet.Fields("invoiceClaim").Value
            decimals = RecSet.Fields("decimals").Value
            salesTax = RecSet.Fields("salesTax").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If CInt(invoiceClaim) = 0 Then 
            Response.Redirect("settleInvoice.aspx")
        End IF
        If IsDBNull(invoiceCreditAmount) = true Then 
            invoiceCreditAmount = 0
        End IF
        salesTax = salesTax + 100
        salesTax = CDbl(salesTax) / 100
        Response.Write("salesTax = " & salesTax & "<br>")
        invoiceClaim = FormatNumber(CDbl(invoiceClaim), decimals)
        invoiceClaim = CDbl(invoiceClaim)
        Response.Write("paymentAmount = " & paymentAmount & "<br>")
        paymentAmount = CDbl(paymentAmount) / CDbl(salesTax)
        Response.Write("paymentAmount = " & paymentAmount & "<br>")
        paymentAmount = FormatNumber(CDbl(paymentAmount), decimals)
        Response.Write("paymentAmount = " & paymentAmount & "<br>")
        paymentAmount = CDbl(paymentAmount)
        Response.Write("paymentAmount = " & paymentAmount & "<br><br>")
        Response.Write("invoiceCreditAmount = " & invoiceCreditAmount & "<br>")
        Response.Write("invoiceClaim = " & invoiceClaim & "<br>")
        Response.Write("invoiceClaim = " & invoiceClaim & "- " & paymentAmount & " - " & invoiceCreditAmount & "<br>")
        invoiceClaim = CDbl(invoiceClaim) - CDbl(paymentAmount) - CDbl(invoiceCreditAmount)
        Response.Write("invoiceClaim = " & invoiceClaim & "<br>")
        invoiceClaim = FormatNumber(CDbl(invoiceClaim), decimals)
        invoiceClaim = Replace(CStr(invoiceClaim), ",", ".")
        Response.Write("invoiceClaim = " & invoiceClaim & "<br><br>")
        If CInt(invoiceClaim) < 10 Then 
            invoiceSettled = 1
        Else
            invoiceSettled = 0
        End IF
        Response.Write("invoiceSettled = " & invoiceSettled & "<br>")
        paymentDateYear = CLng(paymentDateYear)
        paymentDateMonth = CLng(paymentDateMonth)
        paymentDateDay = CLng(paymentDateDay)
        If CLng(paymentDateMonth) < 10 Then 
            paymentDateMonth = "0" & paymentDateMonth & ""
        End IF
        If CLng(paymentDateDay) < 10 Then 
            paymentDateDay = "0" & paymentDateDay & ""
        End IF
        paymentDate = paymentDateYear & "-" & paymentDateMonth & "-" & paymentDateDay
        Response.Write("invoiceID = " & invoiceID & "<br>")
        Response.Write("invoiceClaim = " & invoiceClaim & "<br>")
        Response.Write("paymentAmount = " & paymentAmount & "<br>")
        Response.Write("paymentDate = " & paymentDate & "<br>")
        Response.Write("invoiceSettled = " & invoiceSettled & "<br>")
        dbOpen()
        Conn.Execute("Update tblInvoice Set invoiceClaim = " & invoiceClaim & ", invoiceSettled = " & invoiceSettled & ", paymentDate = '" & paymentDate & "' Where invoiceID = " & invoiceID & "")
        Conn.Execute("Update tblInvoicePriceListModification Set invoiceClaim = " & invoiceClaim & ", invoiceSettled = " & invoiceSettled & ", paymentDate = '" & paymentDate & "' Where invoicePriceListModificationID = " & invoiceID & "")
        Conn.Execute("Update tblInvoiceDurationPenalty Set invoiceClaim = " & invoiceClaim & ", invoiceSettled = " & invoiceSettled & ", paymentDate = '" & paymentDate & "' Where invoiceDurationPenaltyID = " & invoiceID & "")
        Conn.Execute("Update tblInvoiceDistancePenalty Set invoiceClaim = " & invoiceClaim & ", invoiceSettled = " & invoiceSettled & ", paymentDate = '" & paymentDate & "' Where invoiceDistancePenaltyID = " & invoiceID & "")
        Conn.Execute("Update tblInvoiceAdminFee Set invoiceClaim = " & invoiceClaim & ", invoiceSettled = " & invoiceSettled & ", paymentDate = '" & paymentDate & "' Where invoiceAdminFeeID = " & invoiceID & "")
        Conn.Execute("Update tblInvoiceServiceAccountBalance Set invoiceClaim = " & invoiceClaim & ", invoiceSettled = " & invoiceSettled & ", paymentDate = '" & paymentDate & "' Where invoiceServiceAccountBalanceID = " & invoiceID & "")
        Conn.Execute("Update tblInvoiceCreditServiceAccountBalance Set invoiceClaim = " & invoiceClaim & ", invoiceSettled = " & invoiceSettled & ", paymentDate = '" & paymentDate & "' Where invoiceCreditServiceAccountBalanceID = " & invoiceID & "")
        Conn.Execute("Update tblInvoiceSeparate Set invoiceClaim = " & invoiceClaim & ", invoiceSettled = " & invoiceSettled & ", paymentDate = '" & paymentDate & "' Where invoiceSeparateID = " & invoiceID & "")
        Conn.Execute("Update tblInvoiceCreditSeparate Set invoiceClaim = " & invoiceClaim & ", invoiceSettled = " & invoiceSettled & ", paymentDate = '" & paymentDate & "' Where invoiceCreditSeparateID = " & invoiceID & "")
        dbClose()
        If invoiceSettled = 1 Then 
            dbOpen()
            Conn.Execute("Update tblInvoiceCredit Set invoiceClaim = 0, invoiceSettled = 1, paymentDate = '" & paymentDate & "' Where invoiceID = " & invoiceID & "")
            Conn.Execute("Update tblInvoiceCreditDistancePenalty Set invoiceClaim = " & invoiceClaim & ", invoiceSettled = " & invoiceSettled & ", paymentDate = '" & paymentDate & "' Where invoiceCreditDistancePenaltyID = " & invoiceID & "")
            Conn.Execute("Update tblInvoiceCreditDurationPenalty Set invoiceClaim = " & invoiceClaim & ", invoiceSettled = " & invoiceSettled & ", paymentDate = '" & paymentDate & "' Where invoiceCreditDurationPenaltyID = " & invoiceID & "")
            dbClose()
        End IF
        Response.Redirect("settleInvoice.aspx?subDo=%20-%20SETTLED")
    ElseIf Request.QueryString("do") = "changeInvoiceID" Then 
        invoiceID = Request.Form("invoiceID")
        If Len(invoiceID) > 0 Then 
            invoiceID = CLng(invoiceID)
            Response.Redirect("settleInvoice.aspx?invoiceID=" & invoiceID & "")
        Else
            Response.Redirect("settleInvoice.aspx")
        End IF
    End IF
%>
